package com.phpxiu.app.view.custom;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.utils.KKYUtil;

/**
 * Created by 吴金洲 on 2016/7/20
 */
public class FlyWordView extends RelativeLayout implements Animator.AnimatorListener {
    private int index=-1;
    private AnimatorSet animationSet;
    private ObjectAnimator flyAnim;
    private FlyWordViewAnimEndListener endListener;
    private int mScreenWidth;
    private int mWidth;
    private int mHeight;
    private int mTopMargin=0;
    private FrescoImageView headView;
    private TextView nickView,msgView;
    private boolean isFlying=false;//正在执行动画
    public FlyWordView(Context context) {
        super(context);
    }

    public FlyWordView(Context context,int index) {
        this(context);
        init(context,index);
    }

    public FlyWordView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FlyWordView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(21)
    public FlyWordView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private final void init(Context context,int index){
        this.index=index;
        mScreenWidth=context.getResources().getDisplayMetrics().widthPixels;
        LayoutInflater mInflater=LayoutInflater.from(context);
        mHeight= KKYUtil.dip2px(context, 37);
        mTopMargin=KKYUtil.dip2px(context,3.5f);
        addView(mInflater.inflate(R.layout.fly_word_text_view,null),new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        LayoutParams params=new LayoutParams(mHeight,mHeight);
        params.topMargin=mTopMargin;
        addView(mInflater.inflate(R.layout.fly_word_image_view,null),params);
        headView=(FrescoImageView)findViewById(R.id.fly_word_head_view);
        nickView=(TextView)findViewById(R.id.fly_word_nick_view);
        msgView=(TextView)findViewById(R.id.fly_word_msg_view);
        animationSet=new AnimatorSet();
        animationSet.addListener(this);
        flyAnim=ObjectAnimator.ofFloat(this,"translationX",mScreenWidth,-mScreenWidth);
        animationSet.play(flyAnim);
        animationSet.setDuration(15000);
        animationSet.setInterpolator(new LinearInterpolator());
        setVisibility(INVISIBLE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //获取本身的宽高 这里要注意,测量之后才有宽高
        mWidth = getMeasuredWidth();
    }

    /**
     * 更新状态
     */
    public void updateState(boolean isFlying){
        this.isFlying=isFlying;
    }

    /**
     * 正在飞行
     * @return
     */
    public boolean isFlying(){
        return isFlying;
    }

    /**
     * 更新头像,昵称，消息,准备飞行
     * @param avatar 头像图片url
     * @param nick
     * @param msg
     */
    public void prepareFly(String avatar,String nick,String msg){
        if(headView!=null){
            headView.setImageURI(Uri.parse(avatar));
        }
        if(nickView!=null){
            nickView.setText(nick);
        }
        if(msgView!=null){
            msgView.setText(msg);
        }
        fly();
    }

    /**
     * 飞出动画
     */
    public void fly(){
        flyAnim.setFloatValues(mScreenWidth,-(mScreenWidth+mWidth));
        animationSet.start();
        //Toast.makeText(getContext(),"运行动画："+ index,Toast.LENGTH_LONG).show();
    }

    /**
     * 添加动监听
     * @param listener
     */
    public void addAnimEndListener(FlyWordViewAnimEndListener listener){
        endListener=listener;
    }

    /**
     * 获取头像高度
     * @return
     */
    public int getHeadHeight(){
        return mHeight+mTopMargin;
    }

    @Override
    public void onAnimationStart(Animator animation) {
        if(getVisibility()!=VISIBLE){
            setVisibility(VISIBLE);
        }
    }

    @Override
    public void onAnimationEnd(Animator animation) {
        if(headView!=null){
            headView.setImageURI(Uri.parse("res:///"+R.drawable.def_user_icon));
        }
        if(endListener!=null){
            endListener.onFlyWordAnimEnd(this.index);
        }
    }

    @Override
    public void onAnimationCancel(Animator animation) {

    }

    @Override
    public void onAnimationRepeat(Animator animation) {

    }

    /**
     * 销毁
     */
    public void destroy(){
        animationSet.removeAllListeners();
        animationSet=null;
        flyAnim=null;
        endListener=null;
    }

    /**
     * 结束回调
     */
    public interface FlyWordViewAnimEndListener {
        void onFlyWordAnimEnd(int index);
    }
}
